#include <stdio.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>

char *match_generator(const char *text, int state)
{
	static int list_index;
	char *a[5] = {"abc", "acb", "abd", "bcd", NULL};

	if (!state)
		list_index = 0;
	else list_index++;
	if (a[list_index]) {
		while (a[list_index]) {
			if (!strncmp(a[list_index], text, strlen(text))) {
				return strdup(a[list_index]);
			}
			else list_index++;
		}
		return NULL;
	}
	else return NULL;
}

char **attempted(const char *text, int start, int end)
{
	char **matches = NULL;

	matches = rl_completion_matches(text, match_generator);
	
	return matches;
}

int main(int argc, char **argv)
{
	char *line = NULL;

	rl_readline_name = "(ipmsg)";
	rl_attempted_completion_function = attempted;

	while (1) {
		line = readline("(ipmsg)");
		if (!line)
			break;
	}
	return 0;
}
